<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Test;

use App\Constant\Database\FriendEnum;
use App\Controller\AbstractController;
use App\Helper\HashIdsHelper;
use App\Model\Friend;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

use function Hyperf\Config\config;

#[Controller('/test')]
class IndexController extends AbstractController
{
    #[RequestMapping(path: 'select', methods: 'get')]
    public function select()
    {
        return Friend::query()
            ->where('type', FriendEnum::TYPE_APPLY)
            ->where('status', FriendEnum::STATUS_CHECK)
            ->whereNotIn('friend', array_keys(config('assistant')))
            ->where('created_at', '<', date('Y-m-d H:i:s', time() - 3 * 86400))
            ->get();
    }

    #[RequestMapping(path: 'index', methods: 'get,post')]
    public function index()
    {
        auth()->user();
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        logInfo('FooAspect', 'index/index', 'index_index');
        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    #[RequestMapping(path: 'test', methods: 'get')]
    public function test()
    {
        $user = $this->request->input('user', 'Hyperf');
        $id = $this->request->input('id', 10);
        $method = $this->request->getMethod();
        $encode = HashIdsHelper::encode($id);
        $decode = HashIdsHelper::decode($encode);
        logInfo('FooAspect', 'index/test', 'index_test');
        return [
            'encode' => $encode,
            'decode' => $decode,
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    #[RequestMapping(path: 'redis_subscribe', methods: 'POST')]
    public function redisSubscribe()
    {
        $event = $this->request->input('event');
        $data = $this->request->input('data');
        $message = [
            'uuid' => uniqid(strval(mt_rand(0, 1000))),
            'event' => $event,
            'data' => $data,
            'options' => [],
        ];
        return redis()->publish('websocket', is_string($message) ? $message : json_encode($message));
        return $message;
    }

    #[RequestMapping(path: 'mkdir', methods: 'POST')]
    public function test2()
    {
        $fileType = $this->request->input('fileType');
        $date = date('YmdH');
        $fileDir = "runtime/uploads/{$fileType}/{$date}";
        // 判断文件夹是否存在
        if (! is_dir($fileDir)) {
            mkdir($fileDir, 0777, true);
            chmod($fileDir, 0777);
        }
    }
}
